
import { _decorator, Component, EventTouch, Label, Node, Sprite } from 'cc';
import { GameData } from '../system/GameData';
import { PopManager } from '../manager/PopManager';
import { AppControll, EventName, PopView } from '../manager/AppControll';
import EvtManger from '../tools/EvtManger';
import { DataHelper } from '../system/DataHelper';
import { SoundManager } from '../manager/SoundManger';
import { unlockType } from '../pop/Skin';
const { ccclass, property } = _decorator;


@ccclass('autoFightBtn')
export class autoFightBtn extends Component {
    private mSprite: Sprite = null;
    private mLbSpeed: Label = null;
    protected onLoad(): void {
        this.mSprite = this.node.getComponent(Sprite);
        this.mLbSpeed = this.node.getChildByName("atkSpeed").getComponent(Label);;
        this.node.on(Node.EventType.TOUCH_END, this.OnTouchEnd, this);
        EvtManger.on(EventName.automaticUpgrade, this, this.InitView);
        EvtManger.on(EventName.unlockSkin, this, this.InitView);
        this.InitView();
    }

    protected onDestroy(): void {
        EvtManger.off(EventName.automaticUpgrade, this, this.InitView);
    }

    InitView() {
        if (GameData.playerData.MGAutomaticAttackLv == 0) {
            this.mSprite.grayscale = true;
            this.mLbSpeed.node.active = false;
            this.node.getChildByName("levelInfo").active = true;
            this.node.getChildByName("levelUp").active = false;
        } else {
            this.mSprite.grayscale = false;
            this.mLbSpeed.node.active = true;
            this.node.getChildByName("levelInfo").active = false;
            this.node.getChildByName("levelUp").active = true;
            let arr = GameData.playerData.skin;
            let attackInterval = 0;
            arr.forEach(id => {
                let config = DataHelper.GetSkin_configById(id);
                switch (config.unlockAttrType) {
                    case unlockType.AttackInterval:
                        attackInterval += config.unlockNum;
                        break;
                }
            })
            let value = (DataHelper.GetAutoSpeed().attributeNum - attackInterval);
            let str = "";
            if (value < 0.2) {
                str = value.toFixed(2);
            } else {
                str = value.toFixed(1);
            }
            this.mLbSpeed.string = str + "";
        }
        if (!DataHelper.GetAutoSpeedByLv(GameData.playerData.MGAutomaticAttackLv + 1)) {
            this.node.getChildByName("levelInfo").active = false;
            this.node.getChildByName("levelUp").active = false;
        }
    }

    OnTouchEnd(event: EventTouch) {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        PopManager.Instance<PopManager>().ShowPop(PopView.autoFight);
    }

    start() {
    }
}

